%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% analyzeVideoInput.m 
% createMissingDirs.m 
% generateOutputDirectory.m 
% getBitDepthFromVideoObject.m
% getFileSequenceFromRepresentative.m 
% getImagesFromImages.m 
% getImagesFromMovie.m 
% loadPresetsToProject.m 
% mergeRangeIntoSeparatedRanges.m 
% patientlyKeepTryingToReadUntilErrorFree.m 
% readAndCheckVideoFrames.m 
% readImageFile.m 
% readImagesByIndexRange.m  
% readVideoFrames.m 
% readdpx.m 
% secureReadableImages.m 
% setUpFactoryPresets.m 
% setUpProjectDirectories.m 
% setUpProjectWithFfmpegReadableMovieOrImage.m 
% setUpProjectWithMatlabReadableMovie.m 
% setUpProjectWithMatlabReadableOrDpxImage.m 
% Image processing toolbox
% ffmpeg
%
%==========================================================================

%==========================================================================
% The function "analyzeSource" figures out whether the user-specified
% source is a project file or a video file and acts accordingly to prepare
% a project structure holding as much information relevant to optical sound
% extraction as available.
% INPUT: "sourceName" is a string specifying the source file name
% (including a full directory path).
% OUTPUT: "projectStruct" returns a project structure read from a project
% file or newly created.
%==========================================================================
function projectStruct = analyzeSource(sourceName)

% If the source is a project file, then just load it. Otherwise, create a
% new project and try the source as a movie or a template image file.
aeoProjectExt = 'mat';
if ( (numel(sourceName) >= numel(aeoProjectExt) + 1)&& ...
        (strcmpi(sourceName((end - numel(aeoProjectExt)):end), ['.' aeoProjectExt])) )
    % Load the existing project.
    load(sourceName);
    
    % Check if the loaded structure appears to be a project structure.
    if (exist('projectStruct', 'var') ~= 1)
        error('SoundRestoration:analyzeSource:SourceIsNotValidProject', ...
            'Specified source is not a valid project file.');
    end
else
    % Create a .mat file storing the factory presets.
    [~, factoryPresetsFileName] = setUpFactoryPresets();
    % Set up a preset project structure.
    projectStruct = loadPresetsToProject(factoryPresetsFileName);
    
    % Try to read the movie or identify the image files matching the given
    % template.
    projectStruct = analyzeVideoInput(sourceName, projectStruct);
    
    % If the specified input file does not exist, then finish here.
    if (strcmpi(projectStruct.inFile.kind, 'not existing') == true)
        % Pop up a warning dialog box.
        warndlg({'The specified input file', '', sourceName, '', 'does not exist.'}, 'Check input file name');
        return;
    end
end

% Initialize the desired dimensions of the exported video with the
% dimensions of the original source.
projectStruct.exportHeight = projectStruct.height;
projectStruct.exportWidth = projectStruct.width;

return;
% end of the function "analyzeSource"
